﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class admin1_ManagerUser : System.Web.UI.Page
{
    eStoreDataContext db = new eStoreDataContext();
    protected void Page_Load(object sender, EventArgs e)
    {
        rpt_Load.DataSource = db.SiteUsers;
        rpt_Load.DataBind();
    }
    protected void btnInsert_Click(object sender, EventArgs e)
    {
        try
        {
            var us = new SiteUser
                {
                    UserName = txt_Name.Text,
                    Password = PasswordHash.PasswordHash.CreateHash(txt_Password.Text),
                    FullName = txt_Fullname.Text,
                    Job = txt_Job.Text,
                    Description = txt_Description.Text
                };
            db.SiteUsers.InsertOnSubmit(us);
            db.SubmitChanges();
            panel_Add.Visible = false;
            txt_Fullname.Text = txt_Name.Text = txt_Job.Text = txt_Description.Text = "";
            Response.Redirect("ManagerUser.aspx");
        }
        catch (Exception ex)
        {
            if (ex.Message.Contains("Cannot add an entity with a key that is already in use"))
            {
                Response.Write("<script> alert('Tên tài khoản đã tồn tại! Vui lòng chọn tên khác!');</script>");
            }
        }
    }
    protected void lbtn_Edit_Click(object sender, EventArgs e)
    {
        panel_Edit.Visible = true;
         string usernam = (((LinkButton)sender).CommandArgument.ToString());
        var site=db.SiteUsers.Single(s=>s.UserName==usernam);
        txt_Username_Edit.Text = site.UserName;
        txt_Fullname_Edit.Text = site.FullName;
        txt_Job_Edit.Text = site.Job;
        txt_Description_Edit.Text = site.Description;
    }
    protected void btn_Save_Edit_Click(object sender, EventArgs e)
    {
        var user = db.SiteUsers.Single(s => s.UserName == txt_Username_Edit.Text);
        user.FullName = txt_Fullname_Edit.Text;
        user.Job = txt_Job_Edit.Text;
        user.Description = txt_Description_Edit.Text;
        db.SubmitChanges();
        Response.Redirect("ManagerUser.aspx");
    }
    protected void lbtn_Del_Click(object sender, EventArgs e)
    {
         string usernam = (((LinkButton)sender).CommandArgument.ToString());
        if(usernam!="")
        {
       
        //Response.Write("<script>alert("+id+")</script>"); kiem tra id 
        //1. tim
            var tou = db.SiteUsers.Single(c => c.UserName == usernam);
            //2. Xoa
            db.SiteUsers.DeleteOnSubmit(tou);
            //3. luu
            db.SubmitChanges();
            Response.Redirect("ManagerUser.aspx");
        }
        else
        {
            Response.Write("<script> alert('Xóa user không thành công! User này của bạn!');</script>");
        }
    }

    
    protected void lbt_Close_Edit_Click(object sender, EventArgs e)
    {
        panel_Edit.Visible = false;
    }
    protected void lbt_ClosAdd_Click(object sender, EventArgs e)
    {
        panel_Add.Visible = false;
    }

    protected void lbt_Add_New_Click(object sender, EventArgs e)
    {
        panel_Add.Visible = true;
    }
}